package com.ruoyi.project.sales.orders.divide.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sales.orders.divide.domain.OrderType;
import com.ruoyi.project.sales.orders.divide.service.OrderTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sales/orders/divide")
public class OrderTypeController extends BaseController {

    private String prefix = "sales/orders/divide";

    @Autowired
    private OrderTypeService orderTypeService;

    @GetMapping("")
    public String operlog()
    {
        return prefix + "/divide";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderType orderType)
    {
        startPage();
        List<OrderType> list = orderTypeService.selectOrderTypeList(orderType);
        return getDataTable(list);
    }

    /**
     * 新增订单类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated OrderType orderType)
    {
        if (UserConstants.ORDERTYPE_NAME_NOT_UNIQUE.equals(orderTypeService.checkTypeNameUnique(orderType)))
        {
            return error("新增类型'" + orderType.getTname() + "'失败，类型名称已存在");
        }

        return toAjax(orderTypeService.insertType(orderType));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") Long tid, ModelMap map)
    {
        map.put("type", orderTypeService.selectTypeById(tid));
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated OrderType orderType)
    {
        /*if (UserConstants.TYPE__NAME_NOT_UNIQUE.equals(noticeTypeService.checkTypeNameUnique(noticeType)))
        {
            return error("修改类型'" + noticeType.getTname() + "'失败，类型名称已存在");
        }*/

        return toAjax(orderTypeService.updateType(orderType));
    }


    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            System.out.println(ids);
            return toAjax(orderTypeService.deleteTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验订单名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(OrderType  orderType)
    {
        return orderTypeService.checkTypeNameUnique(orderType);
    }
}
